<template>
  <el-dialog
    :title="titleDialog"
    :visible="showDialog"
    @close="close"
  >
    <el-form
      ref="form"
      :model="form"
      label-width="80px"
      :rules="rules"
    >
      <el-form-item
        label="学科名称"
        prop="subjectName"
      >
        <el-input
          v-model="form.subjectName"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="是否显示">
        <el-switch
          v-model="form.isFrontDisplay"
          active-color="#13ce66"
          inactive-color="#ff4949"
        >
        </el-switch>
      </el-form-item>
    </el-form>
    <div
      slot="footer"
      class="dialog-footer"
    >
      <el-row
        type="flex"
        justify="end"
      >
        <el-button @click="close">取 消</el-button>
        <el-button
          type="primary"
          @click="btnOk"
        >确 定</el-button>
      </el-row>
    </div>
  </el-dialog>
</template>

<script>
// 导入接口
import { update, add, detail } from '@/api/hmmm/subjects.js'
export default {
  name: 'subJects-add',
  data () {
    return {
      form: {
        subjectName: '',
        isFrontDisplay: 1
      },
      // value: true,
      rules: {
        subjectName: [
          { required: true, message: '学科名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    node: {
      type: Object,
      default: null
    }
  },
  computed: {
    titleDialog () {
      return this.form.id ? '修改学科' : '新增学科'
    }
  },
  methods: {
    // 确定按钮的点击事件
    async btnOk () {
      try {
        // 手动校验
        await this.$refs.form.validate()
        if (this.form.id) {
          // 修改
          await update(this.form)
        } else {
          // 新增
          // 调用接口
          await add({ ...this.form, isFrontDisplay: this.form.isFrontDisplay ? 1 : 0 })
        }

        // 提示成功
        this.$message.success('操作成功')
        // 关闭弹层
        this.$emit('update:showDialog', false)
        // 重新渲染页面
        this.$emit('update')
      } catch (error) {
        console.log(error)
      }
    },

    // 取消按钮的点击事件
    close () {
      // 重置数据
      this.form = {
        subjectName: '',
        isFrontDisplay: 1
      }
      // 重置校验规则
      this.$refs.form.resetFields()
      // 关闭弹层
      this.$emit('update:showDialog', false)
    },

    // 获取当前用户详情
    async getData (data) {
      const res = await detail(data)
      this.form = res.data
    }
  }
}
</script>

<style scoped lang='scss'></style>
